package com.feonix.common;

import java.io.File;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.feonix.common.bean.PlatformInfo;
import com.feonix.common.permission.PermissionAdapterHander;
import com.feonix.common.permission.PermissionConfig;
import com.feonix.common.permission.ValidModal;
import com.feonix.common.permission.WorkingModal;
import com.feonix.common.util.FreemarkerUtil;
import com.feonix.common.util.Properties;
import com.feonix.common.util.StringUtil;
import com.feonix.common.util.xml.XmlObject;
import com.thoughtworks.xstream.converters.enums.EnumToStringConverter;

public class BaseInfo {

	//private static final Log logger = LogFactory.getLog(BaseInfo.class);
	private static String realPath;
	private static String domain;
	private static String contextPath;

	/**
	 * 以end 结尾的
	 */
	private static String classPath;
	private static String pagesPath;
	public static final int ORACLE = 1;
	public static final int MSSQL = 2;
	public static final int DB2 = 3;
	public static final int SYBASE = 4;
	public static final int MYSQL = 5;
	public static final int KINGBASEES = 6;
	private static int dbType = 1;

	private static boolean reg = false;

	private static boolean init = false;

	private static boolean prepared = false;
	private static String appname;
	private static String version;
	private static String revise;
	private static boolean kick = false;

	private static boolean sso = false;

	private static boolean sync = false;

	private static Integer debug = Integer.valueOf(1);

	private static String pkColumnName = "iid";

	public static void init(ServletContext servletContext) {
		realPath = StringUtil.convertPath(servletContext.getRealPath(""), false);
		contextPath = StringUtil.convertPath(servletContext.getContextPath(), false);
		pagesPath = realPath + "/WEB-INF/pages";
		classPath = BaseInfo.class.getClassLoader().getResource("").getPath();
		loadSystemConfig();
	}

	public static void loadSystemConfig() {
		initPlatform();
		String setupPath = classPath + "pub/config/setup.properties";
		String versionPath = classPath + "version.properties";
		Properties setupProperties = new Properties(setupPath);
		Properties versionProperties = new Properties(versionPath);

		dbType = setupProperties.getInt("dbtype");
		init = BooleanUtils.toBoolean(setupProperties.getInt("init"), 1, 0);
		kick = BooleanUtils.toBoolean(setupProperties.getInt("kick"), 1, 0);
		pkColumnName = setupProperties.getString("pkColumnName", "iid");
		debug = Integer.valueOf(setupProperties.getInt("debug", 1));
		prepared = setupProperties.getBoolean("prepared");
		appname = versionProperties.getString("appname");
		version = versionProperties.getString("version");
		revise = versionProperties.getString("revise");
		String str3 = setupProperties.getString("domain");
		if (StringUtil.isNotEmpty(str3)) {
			str3 = str3.trim();
			if (str3.toLowerCase().startsWith("http")) {
				if (str3.endsWith("/")) {
					str3 = str3.substring(0, str3.length() - 1);
				}
				domain = str3;
			}
		}
	}

	private static void initPlatform() {
		File file = new File(classPath + "pub/platform.xml");
		XmlObject xmlObject = new XmlObject();
		xmlObject.read(file);
		xmlObject.addConverter(new EnumToStringConverter(ValidModal.class, ValidModal.getMap()));
		xmlObject.addConverter(new EnumToStringConverter(WorkingModal.class, WorkingModal.getMap()));
		xmlObject.setNodeToClass("platform", PlatformInfo.class);
		xmlObject.setNodeToField("permissions", PlatformInfo.class, "permissionConfigs");
		xmlObject.setNodeToClass("permission", PermissionConfig.class);
		PlatformInfo platformInfo = (PlatformInfo) xmlObject.getObject();
		boolean bool = platformInfo.isDebug();
		FreemarkerUtil.setDebug(bool);
		List<PermissionConfig> permissionConfigs = platformInfo.getPermissionConfigs();
		for (PermissionConfig permissionConfig : permissionConfigs) {
			PermissionAdapterHander.add(permissionConfig.getName(), permissionConfig.getAdapter());
			PermissionAdapterHander.setValidModal(permissionConfig.getName(), permissionConfig.getValidModal());
		}
	}

	/**
	 * 是否初始化（非数据库）
	 * 
	 * @return
	 */
	public static boolean isPrepared() {
		return prepared;
	}

	public static String getRealPath() {
		return realPath;
	}

	public static String getContextPath() {
		return contextPath;
	}

	public static int getDbType() {
		return dbType;
	}

	public static boolean isReg() {
		return reg;
	}

	public static String getAppName() {
		return appname;
	}

	public static boolean isKick() {
		return kick;
	}

	public static boolean isSso() {
		return sso;
	}

	public static boolean isSync() {
		return sync;
	}

	public static String getPagesPath() {
		return pagesPath;
	}

	public static String getVersion() {
		return version;
	}

	public static String getRevise() {
		return revise;
	}

	public static void setRevise(String paramString) {
		revise = paramString;
	}

	public static Integer getLogLevel() {
		return debug;
	}

	public static String getClassPath() {
		return classPath;
	}

	public static String getPkColumnName() {
		return pkColumnName;
	}

	public static String getDomain() {
		return domain;
	}

	public static String getOS() {
		return System.getProperty("os.name");
	}

	public static boolean isWin() {
		String str = getOS();
		boolean isWin = false;
		if (str.toLowerCase().startsWith("win")) {
			isWin = true;
		}
		return isWin;
	}

}
